home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9601 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  60 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: confusion between putchar and printf
  5. Date: 11 Mar 1996 14:13:59 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4i28j7INN65d@keats.ugrad.cs.ubc.ca>
  8. References: <4i1v2n$30o@news.azstarnet.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4i1v2n$30o@news.azstarnet.com>,
  12. Howard Salmon  <captarm@azstarnet.com> wrote:
  13. >K & R (section 1.5) states that "putchar(c) prints the contents of the 
  14. >integer variable c as a character, usually on the screen".
  15.  ^^^^^^^^^^^^^^^^        ^^^^^^^^^
  16.  
  17. >Here's a small program that I wrote testing putchar.  Why doesn't it 
  18. >compile?
  19. >
  20. >#include <stdio.h>
  21. ># define A "hello world!"
  22. >
  23. >main()
  24. >{
  25. >    int c;
  26. >    c =  A;
  27. >    putchar(A);
  28. >}
  29.  
  30. Since when can you assign a string literal constant such as "hello world!" to
  31. an _integer_ variable? 
  32.  
  33. The C language has a concept of type. The values you assign to a variable
  34. must have a type that is compatible with the variable's type. 
  35.  
  36. Exactly what are you expecting this program to do?  A single call to putchar is
  37. incapable of emitting a string no matter what argument you give to it. It
  38. prints a single character, exactly as K&R says. You can never get a single call
  39. to putchar to place a string on the screen. Try this:
  40.  
  41. #include <stdio.h>
  42.  
  43. int main()
  44. {
  45.     char *p = "hello, world!\n";
  46.  
  47.     /* The string literal "hello, world\n" is stored in static memory.
  48.        The variable p is a character pointer which initially holds the
  49.        address of the character 'h'. */
  50.  
  51.     while (*p)    /* '*p' refers to the character pointed at by p    */
  52.             /* we loop while this is not the zero character    */
  53.         putchar(*p++);    /* print that character,  & increment p    */
  54.                 /* to point to the next character    */
  55.  
  56.     return 0;    /* all C programs need to return an exit status    */
  57. }
  58. -- 
  59.  
  60.